package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.TakeDrugsRecordBean;

import java.util.List;

/**
 * Created by william_sun on 2016-10-06.
 * Description:患者用药记录列表
 */
public class TakeDrugsAdapter extends BaseExpandableListAdapter {

    private List<TakeDrugsRecordBean> datas;
    private Context mContext;
    private LayoutInflater mInflater;

    private final int REFRESH = 222;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH) {
                observer.onChanged();
//                getGroupCount();
//                for (int i = 0; i < getGroupCount(); i++) {
//                    getChildrenCount(i);
//                }
//                notifyDataSetChanged();
//                for (int i = 0; i < datas.size(); i++) {
//                    elv.collapseGroup(i);
//                    elv.expandGroup(i);
//                }
            }
        }
    };

    public TakeDrugsAdapter(Context mContext, List<TakeDrugsRecordBean> datas) {
        this.mContext = mContext;
        this.datas = datas;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setDatas(List<TakeDrugsRecordBean> datas) {
        this.datas = datas;
        handler.sendEmptyMessage(REFRESH);
    }

    private DataSetObserver observer;

    //观察者模式
    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        this.observer = observer;
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        this.observer = null;
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return (groupPosition + 1) * (childPosition + 1);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        viewHolderTitle holderTitle;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_takedrugs_title, null);
            holderTitle = new viewHolderTitle();
            holderTitle.view_divider = convertView.findViewById(R.id.view_takedrugs_titledivider);
            holderTitle.tv_date = (TextView) convertView.findViewById(R.id.tv_takedrugs_date);
            convertView.setTag(holderTitle);
        } else {
            holderTitle = (viewHolderTitle) convertView.getTag();
        }
        if (groupPosition == 0) {
            holderTitle.view_divider.setVisibility(View.GONE);
        } else {
            holderTitle.view_divider.setVisibility(View.VISIBLE);
        }
        holderTitle.tv_date.setText(datas.get(groupPosition).getDate());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        viewHolderInfo holderInfo;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_takedrugs_info, null);
            holderInfo = new viewHolderInfo();
            holderInfo.ll_drug = (LinearLayout) convertView.findViewById(R.id.ll_takedrugs_drugs);
            holderInfo.tv_time1 = (TextView) convertView.findViewById(R.id.tv_takedrugs_time1);
            holderInfo.tv_time2 = (TextView) convertView.findViewById(R.id.tv_takedrugs_time2);
            holderInfo.tv_time3 = (TextView) convertView.findViewById(R.id.tv_takedrugs_time3);
            holderInfo.tv_time4 = (TextView) convertView.findViewById(R.id.tv_takedrugs_time4);
            holderInfo.view_divider = convertView.findViewById(R.id.view_takedrugs_infodivider);
            convertView.setTag(holderInfo);
        } else {
            holderInfo = (viewHolderInfo) convertView.getTag();
        }
        TakeDrugsRecordBean.DrugTime time = datas.get(groupPosition).getList().get(childPosition);

        holderInfo.ll_drug.removeAllViews();
        for (TakeDrugsRecordBean.DrugTake drug : time.getDrugList()) {
            View drugView = mInflater.inflate(R.layout.view_drug, null);
            ((TextView) drugView.findViewById(R.id.tv_viewdrug_drug)).setText(drug.getDrugName());
            ((TextView) drugView.findViewById(R.id.tv_viewdrug_dose)).setText(drug.getDoseName());
            holderInfo.ll_drug.addView(drugView);
        }

        holderInfo.tv_time1.setVisibility(View.INVISIBLE);
        holderInfo.tv_time2.setVisibility(View.INVISIBLE);
        holderInfo.tv_time3.setVisibility(View.INVISIBLE);
        holderInfo.tv_time4.setVisibility(View.INVISIBLE);

        switch (Math.min(time.getTimeList().size(), 4)) {
            case 4:
                holderInfo.tv_time4.setVisibility(View.VISIBLE);
                holderInfo.tv_time4.setText(time.getTimeList().get(3));
            case 3:
                holderInfo.tv_time3.setVisibility(View.VISIBLE);
                holderInfo.tv_time3.setText(time.getTimeList().get(2));
            case 2:
                holderInfo.tv_time2.setVisibility(View.VISIBLE);
                holderInfo.tv_time2.setText(time.getTimeList().get(1));
            case 1:
                holderInfo.tv_time1.setVisibility(View.VISIBLE);
                holderInfo.tv_time1.setText(time.getTimeList().get(0));
                break;
            default:
                break;
        }

        if (childPosition == datas.get(groupPosition).getList().size() - 1) {
            holderInfo.view_divider.setVisibility(View.GONE);
        } else {
            holderInfo.view_divider.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    private class viewHolderTitle {
        private View view_divider;
        private TextView tv_date;
    }

    private class viewHolderInfo {
        private LinearLayout ll_drug;
        private TextView tv_time1;
        private TextView tv_time2;
        private TextView tv_time3;
        private TextView tv_time4;
        private View view_divider;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return childId;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return groupId;
    }
}
